package com.bootdo.wx.service.impl;

import cn.hutool.core.map.MapUtil;
import com.bootdo.common.domain.Tree;
import com.bootdo.common.utils.*;
import com.bootdo.common.utils.excel.MemberMoneyExcelUtils;
import com.bootdo.wx.dao.*;
import com.bootdo.wx.domain.*;
import com.bootdo.wx.service.*;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import org.jsoup.Connection.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import java.awt.image.BufferedImage;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.*;


@Service
public class MemberUserServiceImpl implements MemberUserService {
	@Autowired
	private MemberUserDao memberUserDao;
	@Autowired
	private OrderComService orderComService;
	@Autowired
	private MemberPriceDao memberPriceDao;
	@Autowired
	private MemberCouponDao memberCouponDao;
	@Autowired
	private OrderOrderDao orderOrderDao;
	@Autowired
	public CmsSlideDao cmsSlideDao;
	@Autowired
	public MemberTokenService memberTokenService;
	@Autowired
	public SetImageUploadService setImageUploadService;
	@Autowired
	private CmsContentDao cmsContentDao;
	@Autowired
	private MemberScoreDao memberScoreDao;
	@Autowired
	private MemberSignService memberSignService;
	@Autowired
	private MemberLevelChangeDao memberLevelChangeDao;
	@Autowired
	private GoodsProductDao goodsProductDao;
	@Autowired
	private MemberStockDao memberStockDao;
	@Autowired
	private OrderOrderService orderOrderService;
	@Autowired
	private CmsNoticeDao cmsNoticeDao;
	@Autowired
	private MemberFollowDao memberFollowDao;



	@Override
	public MemberUserDO get(String id){
		return memberUserDao.get(id);
	}

	@Override
	public List<MemberUserDO> list(Map<String, Object> map){
		return memberUserDao.list(map);
	}

	@Override
	public int count(Map<String, Object> map){
		return memberUserDao.count(map);
	}

	@Override
	public int save(MemberUserDO memberUser){
		return memberUserDao.save(memberUser);
	}

	@Override
	public int update(MemberUserDO memberUser){
		return memberUserDao.update(memberUser);
	}

	@Override
	public int remove(String id){
		return memberUserDao.remove(id);
	}

	@Override
	public int batchRemove(String[] ids){
		return memberUserDao.batchRemove(ids);
	}

	/*个人详细信息*/
	@Override
	public Map<String,Object> getMemberCenter(Map<String, Object> params) {
		String userId = params.get("userId").toString();
		MemberUserDO memberUserDO = memberUserDao.get(userId);
		Map<String,Object> map = new HashMap<String,Object>();
		//用信息
		/*map.put("phone",memberUserDO.getPhone());
		map.put("wechat",memberUserDO.getWechat());*/
		if (memberUserDO!=null){
			map.put("wechat",memberUserDO.getWechat());//名字
			map.put("name",memberUserDO.getName());
			map.put("avator",memberUserDO.getAvator());
			map.put("level",memberUserDO.getLevel());
			map.put("babyDays",memberUserDO.getBabyDays());//生日
			map.put("sex",memberUserDO.getSex());
			map.put("phone",memberUserDO.getPhone());
			/*map.put("idcardq",memberUserDO.getIdcardq());
			map.put("idcardh",memberUserDO.getIdcardh());
			map.put("postType", memberUserDO.getPostType());*/   //配送方式
			/*String introUser = memberUserDO.getIntroUser();
			MemberUserDO introUserDO = memberUserDao.get(introUser);
			if (introUserDO == null) {
				map.put("fatherAvator","");
				map.put("fatherName","");
			} else {
				map.put("fatherAvator",introUserDO.getAvator());
				map.put("fatherName",introUserDO.getName());
			}*/
			//积分
		}



		/*BigDecimal scoreNumber = memberScoreDao.getSumByUser(params);
		map.put("scoreNum", scoreNumber); */  //积分
		//签到状态  0：未签到，1：签到
		/*if (memberSignService.getTodaySign(userId).size() != 0) {
			map.put("isCheck", "1");
		} else {
			map.put("isCheck", "0");
		}*/
		//商家地址
		/*String address = cmsContentDao.getByType("shop-address").getValue();
		map.put("shopAddress", address); */  //地址
		//余额
		params.put("income",0);
		Map<String,Object> moneyMap = orderComService.getMemberCom(params);
		map.put("sPrice", moneyMap.get("sPrice"));   //收益
		map.put("couponCount", moneyMap.get("couponCount"));   //优惠券数

		//map.put("allPrice", moneyMap.get("allPrice"));   //余额
		map.put("canExPrice", moneyMap.get("canExPrice"));//可提现余额
		params.remove("type");
		//优惠券数量
		/*List<String> types = new ArrayList<String>();
		types.add("0");
		params.put("types", types);
		int couponNum = memberCouponDao.getMyCouponList(params).size();
		map.put("couponNum", couponNum);   //优惠券总数*/
		//订单个数
		params.remove("types");
		String [] status = new String[10];
		status[0] = "1";
		params.put("status", status);
		int payNum = orderOrderDao.getOrderListByStatus(params).size();
		status[0] = "3";
		int faNum = orderOrderDao.getOrderListByStatus(params).size();
		/*status[0] = "2";
		int ptNum = orderOrderDao.getOrderListByStatus(params).size();*/
		status[0] = "4";
		int shouNum = orderOrderDao.getOrderListByStatus(params).size();
		status[0] = "11";
		int tiNum = orderOrderDao.getOrderListByStatus(params).size();
		status[0] = "7";
		params.put("type", "4");
		int wcNum = orderOrderDao.getOrderListByStatus(params).size();
		status[0] = "5";
		status[1] = "6";
		status[2] = "11";
		params.remove("type");
		int errorNum = orderOrderDao.getOrderListByStatus(params).size();
		map.put("payNum", payNum);   //待支付
		map.put("faNum", faNum);   //待发货
		map.put("shouNum", shouNum);   //待收货
		//map.put("tiNum", tiNum);   //自提
		map.put("errorNum", errorNum);   //错误订单
		map.put("wcNum", wcNum);   //已完成(评价)订单
		//map.put("ptNum", wcNum);   //拼团订单
		//我的粉丝
		params.remove("status");
		int allNum = memberUserDao.getTeamNum(params);   //团队人数
/*		params.put("staTime", format.format(DateUtils.getDayBegin()));
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		int todayNum = memberUserDao.getFansNum(params);   //今天粉丝数
		params.put("endTime", format.format(DateUtils.getDayBegin()));
		params.put("staTime", format.format(DateUtils.getBeginDayOfYesterday()));
		int yesterdayNum = memberUserDao.getFansNum(params);   //昨天粉丝数*/
		map.put("allNum",allNum);
		/*map.put("todayNum",todayNum);
		map.put("yesterdayNum",yesterdayNum);*/
		//库存
		/*Map<String, Object> stockMap = new HashMap<>();
		stockMap.put("userId", userId);
		int findStockNumByUserId = memberStockDao.findStockNumByUserId(stockMap);
		map.put("stock", findStockNumByUserId);*/
		return map;
	}


	@Override
	public Map<String,Object> setUp(Map<String, Object> params) {
		String userId = params.get("userId").toString();
		MemberUserDO memberUserDO = memberUserDao.get(userId);
		Map<String,Object> map = new HashMap<String,Object>();
		if (memberUserDO!=null){
			map.put("wechat",memberUserDO.getWechat());//名字
			map.put("name",memberUserDO.getName());//昵称
			map.put("avator",memberUserDO.getAvator());
			map.put("babyDays",memberUserDO.getBabyDays());//生日
			map.put("sex",memberUserDO.getSex());
			map.put("phone",memberUserDO.getPhone());
		}
		return map;
	}


	@Override
	public PageInfo<Map<String,Object>> getMyFans(Map<String, Object> paramMap) {
		PageHelperUtils.page(paramMap);
		List<Map<String,Object>> list =memberUserDao.getMyFans(paramMap);
		PageInfo<Map<String,Object>> pageInfo = new PageInfo<>(list);
		return pageInfo;
	}

	@Override
	public Map<String,Object> getMyTeam(Map<String, Object> paramMap) {
		String id = paramMap.get("userId").toString();
		MemberUserDO memberUserDO = memberUserDao.get(id);
		Map<String,Object> map = new HashMap<String,Object>();
		if (memberUserDO!=null){
			map.put("name",memberUserDO.getName());
			map.put("avator",memberUserDO.getAvator());
			map.put("level",memberUserDO.getLevel());
			map.put("phone",memberUserDO.getPhone());
		}
		int allNum = memberUserDao.getTeamNum(paramMap);
		map.put("allNum",allNum);
		paramMap.clear();
		paramMap.put("introUser",id);
		paramMap.put("delFlag","0");
		PageHelperUtils.page(paramMap);
		List<MemberUserDO> list = memberUserDao.list(paramMap);
		PageInfo<MemberUserDO> pageInfo = new PageInfo<>(list);
		map.put("pageInfo",pageInfo);
		return map;
	}


	@Override
	public MemberUserDO queryByOpenid(String openId) {
		return memberUserDao.queryByOpenid(openId);
	}

	@Override
	public List<Map<String,Object>> getOrderEcharts(Map<String,Object> map) {
		return memberUserDao.getOrderEcharts(map);
	}

	@Override
	public List<Map<String,Object>> allMoneyEcharts(Map<String,Object> map) {
		return memberUserDao.allMoneyEcharts(map);
	}

	@Override
	public List<Map<String,Object>> allUserEcharts(Map<String,Object> map) {
		return memberUserDao.allUserEcharts(map);
	}

	@Override
	public MemberUserDO getByCode(String code) {
		return memberUserDao.getByCode(code);
	}

	@Override
	public R getExtension(Map<String, Object> params) {
		String userId = params.get("userId").toString();
		Map<String,Object> map = new HashMap<String,Object>();
		MemberUserDO userDO = memberUserDao.get(userId);
		map.put("avator",userDO.getAvator());
		map.put("name", userDO.getName());
		map.put("InvitationCode",userDO.getCode());
		String code = userDO.getCode();
		params.clear();
		params.put("type", "5");
		List<CmsSlideDO> imgList = cmsSlideDao.queryAll(params);
		if (imgList.size() == 0) {
			map.put("imgurl","");
		} else {
			map.put("imgurl",imgList.get(0).getImgUrl());
		}
		if (userDO.getQrcode() != null && !"".equals(userDO.getQrcode())) {
			map.put("qrCode",userDO.getQrcode());
		} else {   //如果库里面没有生成二维码，就生成
			String token = memberTokenService.getToken();
			String url="https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+token;
			Map<String,String> urlMap = new HashMap<String,String>();
			//urlMap.put("access_token",token); 不能传，会报错，微信文档错了
			urlMap.put("scene",code);
			//urlMap.put("page","");     //制定跳小程序的页面，不过不指定，默认跳首页
			try {
				Gson gson = new Gson();
				Response response = HttpUtils.post(url, gson.toJson(urlMap));
				String contentType = response.contentType();
				if (!"image/jpeg".equals(contentType)) {   //没返回图片
					String body = response.body();
					Map<String, Object> resultMap = new HashMap<String, Object>();
					resultMap = gson.fromJson(body, resultMap.getClass());
					System.out.print("获取二维码结果：：" + resultMap.toString());
					return R.error("获取二维码code值："+resultMap.get("errcode").toString());
				}
				//图片上传
				InputStream is = response.bodyStream();
				byte [] imgByte = StringUtils.readInputStream(is);
				BufferedImage image = ImageIO.read(is);
				String fileName = null;
				if (image != null) {//如果image=null 表示上传的不是图片格式
					fileName = "IMG_" + image.getWidth() + "_" + image.getHeight() + "@"+ UUID.randomUUID().toString();;
				}else {
					fileName = UUID.randomUUID().toString();
				}
				String imagePath = new ImgUpload(setImageUploadService).upload(imgByte,
						fileName);
				System.out.println("生成二维码地址地址：："+imagePath);
				MemberUserDO memberUserDO = new MemberUserDO();
				memberUserDO.setId(userId);
				memberUserDO.setQrcode(imagePath);
				memberUserDao.update(memberUserDO);
				map.put("qrCode",imagePath);
			} catch (Exception e) {
				e.printStackTrace();
				return R.error("生成二维码失败");
			}
		}
		return R.ok().put("data",map);
	}

	/**
	 * 根据用户ID查询用户余额详情列表
	 * @param map
	 * @return
	 */
	@Override
	public List<Map<String,Object>> getMoneyListByUser(Map<String,Object> map) {
		String status = map.get("status").toString(); //根据状态显示详情 0冻结余额1可用余额2已返余额
		if("0".equals(status)){
			List<Map<String, Object>> list = memberUserDao.getMoneyListByStatus(map);
			for (Map<String, Object> stringObjectMap : list) {
				stringObjectMap.put("type",3);
			}
			return list;
		}else if("2".equals(status)){
			List<Map<String, Object>> list = memberUserDao.getMoneyListByStatus(map);
			for (Map<String, Object> stringObjectMap : list) {
				stringObjectMap.put("type",3);
			}
			return list;
		}
		return memberUserDao.getMoneyListByUser(map); // 如果不是0或2 就查询可用余额详情列表
	}

	/**
	 * 根据用户ID查询用户余额的总条数
	 * @param map
	 * @return
	 */
	@Override
	public int getMoneyListByUserCount(Map<String, Object> map) {
		String status = map.get("status").toString(); //根据状态显示详情 0冻结 1可用2已返
		if("0".equals(status)){
			return memberUserDao.getMoneyListByStatusCount(map);
		}else if("2".equals(status)){
			return memberUserDao.getMoneyListByStatusCount(map);
		}
		return memberUserDao.getMoneyListByUserCount(map);
	}

	/*导出excel。改变订单导出状态*/
	@Override
	@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED) // 有事务则加入,没有则建立
	public void exportExcel(Map<String,Object> map,List<Map<String,Object>> userList,
							ServletOutputStream out,HttpServletRequest request) {
		//导出excel
		MemberMoneyExcelUtils.exportUserExcel(userList, out);
	}

	@Override
	public List<Map<String,Object>> getMyFansH(Map<String,Object> map) {
		return memberUserDao.getMyFansH(map);
	}

	@Override
	public int getMyFansHCount(Map<String, Object> map) {
		return memberUserDao.getMyFansHCount(map);
	}

	@Override
	public BigDecimal getShoppingNum(Map<String,Object> map) {
		return memberUserDao.getShoppingNum(map);
	}

	@Override
	public List<Map<String,Object>> getShoppingList(Map<String,Object> map) {
		return memberUserDao.getShoppingList(map);
	}

	@Override
	public int getShoppingListCount(Map<String, Object> map) {
		return memberUserDao.getShoppingListCount(map);
	}

	@Override
	@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED) // 有事务则加入,没有则建立
	public void shoppingExportExcel(Map<String,Object> map,List<Map<String,Object>> shoppingList,
									ServletOutputStream out,HttpServletRequest request) {
		//导出excel
		MemberMoneyExcelUtils.exportPTShoppingExcel(shoppingList, out);
	}

	@Override
	public int getZtChildrenNUm(Map<String, Object> map) {
		return memberUserDao.getZtChildrenNUm(map);
	}

	@Override
	@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED) // 有事务则加入,没有则建立
	public R updateLevel(MemberUserDO userDO) {
		String userId = userDO.getId();
		MemberUserDO oldUser = memberUserDao.get(userId);
		memberUserDao.update(userDO);
		//添加等级变动记录
		MemberLevelChangeDO changeDO = new MemberLevelChangeDO();
		changeDO.setId(UuidUtil.get32UUID());
		changeDO.setOldLevel(oldUser.getLevel());
		changeDO.setNewLevel(userDO.getLevel());
		changeDO.setCreateAt(new Date());
		changeDO.setUpdateAt(new Date());
		changeDO.setDelFlag(0);
		changeDO.setComent("后台修改");
		memberLevelChangeDao.save(changeDO);
		//把所有商品规格,都添加到仓库里,用户可以进货
		/*Map<String, Object> productMap = new HashMap<>();
		productMap.put("delFlag", "0");
		List<GoodsProductDO> list = goodsProductDao.list(productMap);
		if(list.size()>0){
			for (GoodsProductDO goodsProductDO : list) {
				productMap.put("goodsId", goodsProductDO.getGoodsId());
				productMap.put("productId", goodsProductDO.getId());
				productMap.put("userId", userId);
				List<MemberStockDO> list2 = memberStockDao.list(productMap);
				if(list2.size()<=0){
					MemberStockDO memberStockDO = new MemberStockDO();
					memberStockDO.setCaAt(new Date());
					memberStockDO.setCause("后台修改");
					memberStockDO.setDelFlag(0);
					memberStockDO.setGoodsId(goodsProductDO.getGoodsId());
					memberStockDO.setId(UUIDUtils.getUUID());
					memberStockDO.setProductId(goodsProductDO.getId());
					memberStockDO.setStock(0);
					memberStockDO.setUserId(userId);
					memberStockDao.save(memberStockDO);
				}
				
			}
		}*/
		return R.ok();
	}

	@Override
	public MemberUserDO findByPhone(String phoneNumber) {

		return memberUserDao.findByPhone(phoneNumber);
	}

	@Override
	public R setMemberDays(Map<String, Object> map) {
		MemberUserDO memberUserDO = memberUserDao.get(map.get("userId").toString());
		Date stringToDate = DateUtils.getStringToDate(map.get("days").toString());
		if(stringToDate.after(new Date())){
			return R.error("设置的停经的天数不能大于当前日期");
		}
		memberUserDO.setBabyDays(map.get("days").toString());
		memberUserDao.update(memberUserDO);
		//计算预产期
		try {
			Date date = DateUtils.getStringToDate(map.get("days").toString());
			int parseInt = Integer.parseInt(cmsContentDao.getByType("baoma_yunqi_days").getValue());
			Date addDayOfDate = DateUtils.addDayOfDate(date,parseInt);
			return R.ok().put("data", DateUtils.format(addDayOfDate, DateUtils.DATE_PATTERN));
		} catch (Exception e) {
			e.printStackTrace();
			return R.error();
		}

	}

	@Override
	public R getOverdue(Map<String, Object> map) {
		if(map.get("userId")==null||"".equals(map.get("userId").toString())){
			return R.ok().put("data", "请登录");
		}
		MemberUserDO memberUserDO = memberUserDao.get(map.get("userId").toString());
		if(memberUserDO==null ){
			return R.error("用户信息有误");
		}else if(memberUserDO.getBabyDays()==null||"".equals(memberUserDO.getBabyDays())){
			return R.ok().put("data", null);
		}else{
			try {
				Date date = DateUtils.getStringToDate(memberUserDO.getBabyDays());
				int parseInt = Integer.parseInt(cmsContentDao.getByType("baoma_yunqi_days").getValue());
				Date addDayOfDate = DateUtils.addDayOfDate(date,parseInt);
				return R.ok().put("data", DateUtils.format(addDayOfDate, DateUtils.DATE_PATTERN));
			} catch (Exception e) {
				e.printStackTrace();
				return R.error();
			}
		}
	}

	@Override
	public List<MemberUserDO> likeList(Map<String, Object> map) {

		return memberUserDao.likeList(map);
	}

	@Override
	public int likeCount(Map<String, Object> params) {
		// TODO Auto-generated method stub
		return memberUserDao.likeCount(params);
	}

	@Override
	public List<MemberUserDO> getUpUser(String id) {
		Map<String, Object> map = new HashMap<>();
		map.put("level", "0");
		map.put("userId", id);
		//map.put("upLevel", "1");
		List<MemberUserDO> myUpVip = memberUserDao.getMyUpVip(map);
		/*if(myUpVip.size()<=0){
			map.put("level", "2");
			List<MemberUserDO> myUpVip1 = memberUserDao.getMyUpVip(map);
			return myUpVip1;
		}else{
			return myUpVip;
		}*/
		return myUpVip;
	}

	@Override
	public List<Map<String, Object>> allUserEchartsByToday(Map<String, Object> params) {
		// TODO Auto-generated method stub
		return memberUserDao.allUserEchartsByToday(params);
	}

	@Override
	public List<Map<String, Object>> getOrderCensus(Map<String, Object> params) {
		// TODO Auto-generated method stub
		return memberUserDao.getOrderCensus(params);
	}

	@Override
	public int getCountOrderCensus(Map<String, Object> params) {
		// TODO Auto-generated method stub
		return memberUserDao.getCountOrderCensus(params);
	}

	@Override
	public Tree<MemberUserDO> getTree(String userId) {
		String[] split = userId.split("&");
		List<Tree<MemberUserDO>> trees = new ArrayList<Tree<MemberUserDO>>();
		/*int level = 0;
		if(!("0").equals(userId)){
			MemberUserDO memberUserDO2 = memberUserDao.get(userId);
			level=memberUserDO2.getLevel();
		}

		List<MemberUserDO> members = memberUserDao.getByLevel(level);*/
		Map<String,Object> map = new HashMap<>();
		map.put("delFlag","0");
		map.put("sort","level");
		map.put("order","desc");
		if(split.length>1&&StringUtils.isNotBlank(split[1])&&!split[1].equals("undefined")){
			map.put("name",split[1]);
		}
		Tree<MemberUserDO> tree1 = new Tree<MemberUserDO>();
		tree1.setId("0");
		tree1.setParentId("0");
		tree1.setText("平台");
		Map<String, Object> state1 = new HashMap<>(16);
		state1.put("opened", false);
		state1.put("mType", "user");
		tree1.setState(state1);
		trees.add(tree1);
		List<MemberUserDO> members = memberUserDao.list(map);
		for (MemberUserDO memberUserDO : members) {
			if(split.length>1&&StringUtils.isNotBlank(split[1])&&!split[1].equals("undefined")){
				//带搜索得
				//获取无限上级
				List<MemberUserDO> infiniteUpUser = memberUserDao.getInfiniteUpUser(memberUserDO.getId());
				HashMap<String, Object> stringObjectHashMap = MapUtil.newHashMap();
				stringObjectHashMap.put("userId",memberUserDO.getId());
				List<MemberUserDO> infiniteDownUser = memberUserDao.getInfiniteDownUser(memberUserDO.getId());

				List<MemberUserDO> allList = new ArrayList<>();
				allList.addAll(infiniteUpUser);
				allList.addAll(infiniteDownUser);
				allList.add(memberUserDO);
				for (MemberUserDO userDO : allList) {
					Tree<MemberUserDO> tree = new Tree<MemberUserDO>();
					tree.setId(userDO.getId());
					tree.setParentId(userDO.getIntroUser());
					tree.setText(userDO.getName());
					Map<String, Object> state = new HashMap<>(16);
					state.put("opened", false);
					state.put("mType", "user");
					tree.setState(state);
					trees.add(tree);
				}



//				tree.setParentId("0");
			}else{
//				tree.setParentId(memberUserDO.getIntroUser());
				Tree<MemberUserDO> tree = new Tree<MemberUserDO>();
				tree.setId(memberUserDO.getId());
				tree.setParentId(memberUserDO.getIntroUser());
				tree.setText(memberUserDO.getName());
				Map<String, Object> state = new HashMap<>(16);
				state.put("opened", false);
				state.put("mType", "user");
				tree.setState(state);
				trees.add(tree);
			}

			/*tree.setText(memberUserDO.getName());
			Map<String, Object> state = new HashMap<>(16);
			state.put("opened", false);
			state.put("mType", "user");
			tree.setState(state);
			trees.add(tree);*/


		}

		// 默认顶级大类为０，根据数据库实际情况调整
		Tree<MemberUserDO> t = BuildTree.build(trees);
		return t;
	}

	/**
	 * 今日收益/月收益
	 * @param params
	 * @return
	 */
	@Override
	public Map<String, Object> getTodayPrice(Map<String, Object> params) {
		params.put("delFlag","0");
		Map<String,Object> map = new HashMap<String,Object>();
		if (!params.containsKey("sTime") && !params.containsKey("eTime")){
			params.put("income",1);//月收益
			Map<String, Object> moneyMap2 = orderComService.getOrderCom(params);
			map.put("yPrice", moneyMap2.get("sPrice"));
			params.put("income",0);//今日收益
			Map<String, Object> moneyMap = orderComService.getOrderCom(params);
			map.put("rPrice", moneyMap.get("sPrice"));
		}else {
			//根据时间收益
			Map<String, Object> moneyMap2 = orderComService.getOrderCom(params);
			map.put("yPrice", moneyMap2.get("sPrice"));
		}
		PageHelperUtils.page(params);
		List<Map<String,Object>> OrderComList = orderComService.comPriceList(params);
		PageInfo<Map<String,Object>> pageInfo = new PageInfo<>(OrderComList);
		map.put("pageInfo",pageInfo);
		return map;
	}

	/**
	 * 累计收益
	 * @param params
	 * @return
	 */
	@Override
	public Map<String, Object> getAllPrice(Map<String, Object> params) {
		params.put("delFlag","0");
		Map<String,Object> map = new HashMap<String,Object>();
		Map<String, Object> moneyMap = orderComService.getOrderCom(params);
		map.put("allPrice", moneyMap.get("sPrice"));
		PageHelperUtils.page(params);
		List<Map<String,Object>> OrderComList = orderComService.comPriceList(params);
		PageInfo<Map<String,Object>> pageInfo = new PageInfo<>(OrderComList);
		map.put("pageInfo",pageInfo);
		return map;
	}

	@Override
	public Map<String, Object> getTeamDetails(Map<String, Object> params) {
		Map<String, Object> map = memberUserDao.getTeamDetails(params);
		return map;

	}

	@Override
	public PageInfo<Map<String,Object>> titleList(Map<String, Object> params) {
		params.put("delFlag","0");
		PageHelperUtils.page(params);
		List<Map<String, Object>> titleList = cmsNoticeDao.titleList(params);
		PageInfo<Map<String,Object>> pageInfo = new PageInfo<>(titleList);
		return pageInfo;
	}

	@Override
	public Map<String, Object> getNoticeDetail(Map<String, Object> params) {
		Map<String, Object> map=new HashMap<>();
		CmsNoticeDO cmsNotice = cmsNoticeDao.get(params.get("id").toString());
		map.put("cmsNotice",cmsNotice);
		return map;
	}

	@Override
	public List<String> getMemberHistory(Map<String, Object> params) {
		List<String> dateList = memberFollowDao.listHistory(params);
		return dateList;
	}

	@Override
	public Map<String, Object> getcomUser(Map<String, Object> params) {
		//总收益
		BigDecimal allComUserMoney = orderComService.sumComUserMoney(params);

		String comUserByUserId = params.get("userId").toString();
		params.remove("userId");
		Map<String,Object> orderMap = orderOrderService.getOrderById(params);

		Map<String,Object> map = new HashMap<String,Object>();


		params.put("comUser",comUserByUserId);
		params.put("delFlag","0");
		List<OrderComDO> list = orderComService.list(params);
		if(list.size()>0){
			//证明是吃差价的人,出过返利,返负数

			//接单费
			BigDecimal sumComUserMoney = orderComService.sumComUserMoney(params);
			List<Map<String,Object>> comInfo = orderComService.comUser(params);
			if(comInfo.size()>0){
				for (Map<String, Object> stringObjectMap : comInfo) {
					String comPrice = MapUtil.getStr(stringObjectMap, "comPrice");
					stringObjectMap.put("comPrice","-"+comPrice);
				}
			}
			orderMap.put("comInfo",comInfo);
			orderMap.put("allComUserMoney",allComUserMoney);//总收益
			orderMap.put("sumComUserMoney",sumComUserMoney.negate());//接单费
			return orderMap;

		}else{
			//吃返利的人,返正数
			params.put("userId", MapUtil.getStr(params,"comUser"));
			params.put("delFlag","0");
			params.remove("comUser");
			BigDecimal sumComUserMoney = orderComService.sumComUserMoney(params);
			List<Map<String,Object>> comInfo = orderComService.comUser(params);
			if(comInfo.size()>0){
				for (Map<String, Object> stringObjectMap : comInfo) {
					String comUser = MapUtil.getStr(stringObjectMap, "comUser");
					if(StringUtils.isBlank(comUser)||comUser.equals("0")){
						stringObjectMap.put("name","平台");
						stringObjectMap.put("level","平台");
						stringObjectMap.put("avator","");
					}else{
						MemberUserDO memberUserDO = memberUserDao.get(comUser);
						stringObjectMap.put("name",memberUserDO.getName());
						stringObjectMap.put("level",memberUserDO.getLevel());
						stringObjectMap.put("avator",memberUserDO.getAvator());

					}
				}
			}
			orderMap.put("comInfo",comInfo);
			orderMap.put("allComUserMoney",allComUserMoney);//总收益
			orderMap.put("sumComUserMoney",sumComUserMoney);//接单费
			return orderMap;

		}


	}


}
